package es.uji.viselab.benchmark.process;

import javax.swing.JFrame;

import org.math.plot.Plot2DPanel;
import org.math.plot.Plot3DPanel;

import com.panayotis.gnuplot.JavaPlot;

import es.uji.viselab.benchmark.database.BMDataType;
import es.uji.viselab.util.LogLevel;
import es.uji.viselab.util.LogRecorder;

public class JPlot extends BenchMarkingProcessor {
	// DBConnection dataBase;
	double[] dblXYZEndEffectorX = null;
	double[] dblXYZEndEffectorY = null;
	double[] dblXYZEndEffectorZ = null;
	double[] dblXYErrorX = null;
	double[] dblXYErrorY = null;
	double[] dblXYVelocityX = null;
	double[] dblXYVelocityY = null;

	public void run() {

		if (dbConnection != null) {
			LogRecorder.log(LogLevel.INFO,
					"Processing data from experiment id: " + dbConnection.getExperimentId());

			double[][] xyzEndEffector = dbConnection.getData(BMDataType.XYZ_END_EFFECTOR);
			if (xyzEndEffector.length>0)
				plot3D("XYZ end effector", xyzEndEffector);

			double[][] error = dbConnection.getData(BMDataType.ERROR);
			if (error.length>0)
				plot2D("Error", error);

		} else {
			LogRecorder.log(LogLevel.ERROR,
					"Cannot process without data (without having a database)", "JPlot.run();");

		}
	}

	void plot() {

		JavaPlot p = new JavaPlot();
		int[][] values = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
		p.addPlot(values);
		p.plot();

		plot3D("XYZ end effector", dbConnection.getData(BMDataType.XYZ_END_EFFECTOR));
		
	}

	private void plot3D(String title, double[][] data) {

		double[] x = new double[data.length];
		double[] y = new double[data.length];
		double[] z = new double[data.length];

		for (int i = 0; i < data.length; i++) {
			x[i] = data[i][0];
			y[i] = data[i][1];
			z[i] = data[i][2];
		}
		plot3D(title, x, y, z);
	}

	private void plot2D(String title, double[][] data) {

		double[] x = new double[data.length];
		double[] y = new double[data.length];

		for (int i = 0; i < data.length; i++) {
			x[i] = data[i][0];
			y[i] = data[i][1];
		}
		plot2D(title, x, y);
	}

	private void plot3D(String title, double[] x, double[] y, double[] z) {
		// create your PlotPanel (you can use it as a JPanel)
		Plot3DPanel plot = new Plot3DPanel();

		// add a line plot to the PlotPanel
		plot.addLinePlot(title, x, y, z);

		// plot.addCloudPlot(name, sampleXYZ, nX, nY, nZ)

		// put the PlotPanel in a JFrame, as a JPanel
		JFrame frame = new JFrame(title);
		frame.setSize(500, 400);
		frame.setContentPane(plot);
		frame.setVisible(true);
	}

	private void plot2D(String title, double[] x, double[] y) {
		// create your PlotPanel (you can use it as a JPanel)
		Plot2DPanel plot = new Plot2DPanel();

		// add a line plot to the PlotPanel
		plot.addLinePlot(title, x, y);

		// put the PlotPanel in a JFrame, as a JPanel
		JFrame frame = new JFrame(title);
		frame.setSize(500, 400);
		frame.setContentPane(plot);
		frame.setVisible(true);
	}

	@Override
	public String getPanelTitle() {
		return "JPlot graphics";
	}

	@Override
	public int panelWidth() {
		return 500;
	}

	@Override
	public int panelHeight() {
		return 400;
	}

}
